import {
	Channel,
	PresenceChannel
} from './channel'
import {
	WeappSocketConnector
} from './connector';

import store from '@/store'
/**
 * This class is the primary API for interacting with broadcasting.
 */
export default class Echo {
	/**
	 * The broadcasting connector.
	 */
	connector;

	/**
	 * The Echo options.
	 */
	options;

	/**
	 * Create a new class instance.
	 */
	constructor(options) {
		this.options = options;
		this.connect();
		this.registerInterceptors();
	}


	/**
	 * Get a channel instance by name.
	 */
	channel(channel) {
		return this.connector.channel(channel);
	}

	/**
	 * Create a new connection.
	 */
	connect() {

		if (this.options.broadcaster == 'pusher') {
			this.connector = new PusherConnector(this.options);
		} else if (this.options.broadcaster == 'socket.io') {
			this.connector = new SocketIoConnector(this.options);
		} else if (this.options.broadcaster == 'weapp.socket.io') {
			this.connector = new WeappSocketConnector(this.options);
		} else if (this.options.broadcaster == 'null') {
			this.connector = new NullConnector(this.options);
		}

	}
	wxSocket() {
		if (this.connector && this.connector.socket) {
			return this.connector.socket
		}
		console.log('出错，没有套接字');
			
	}
	
	/**
	 * Disconnect from the Echo server.
	 */
	disconnect() {
		this.connector.disconnect();
	}

	/**
	 * Get a presence channel instance by name.
	 */
	join(channel) {
		return this.connector.presenceChannel(channel);
	}

	/**
	 * Leave the given channel.
	 */
	leave(channel) {
		this.connector.leave(channel);
	}

	/**
	 * Listen for an event on a channel instance.
	 */
	listen(channel, event, callback) {
		return this.connector.listen(channel, event, callback);
	}

	/**
	 * Get a private channel instance by name.
	 */
	private(channel) {
		return this.connector.privateChannel(channel);
	}

	/**
	 * Get the Socket ID for the connection.
	 */
	socketId() {
		return this.connector.socketId();
	}

	/**
	 * Register 3rd party request interceptiors. These are used to automatically
	 * send a connections socket id to a Laravel app with a X-Socket-Id header.
	 */
	registerInterceptors() {
		if (typeof Vue === 'function' && Vue.http) {
			this.registerVueRequestInterceptor();
		}

		if (typeof axios === 'function') {
			this.registerAxiosRequestInterceptor();
		}

		if (typeof jQuery === 'function') {
			this.registerjQueryAjaxSetup();
		}
		
		if(typeof uni.request === 'function') {
			this.registerUniRequestSetup()
		}
	}
	registerUniRequestSetup() {
		store.commit('updateXSocketID',this.socketId())
	}
	/**
	 * Register a Vue HTTP interceptor to add the X-Socket-ID header.
	 */
	registerVueRequestInterceptor() {
		console.log('vue interceptor');
		Vue.http.interceptors.push((request, next) => {
			if (this.socketId()) {
				request.headers.set('X-Socket-ID', this.socketId());
			}

			next();
		});
	}

	/**
	 * Register an Axios HTTP interceptor to add the X-Socket-ID header.
	 */
	registerAxiosRequestInterceptor() {
		axios.interceptors.request.use((config) => {
			if (this.socketId()) {
				config.headers['X-Socket-Id'] = this.socketId();
			}

			return config;
		});
	}

	/**
	 * Register jQuery AjaxSetup to add the X-Socket-ID header.
	 */
	registerjQueryAjaxSetup() {
		if (typeof jQuery.ajax != 'undefined') {
			jQuery.ajaxSetup({
				beforeSend: (xhr) => {
					if (this.socketId()) {
						xhr.setRequestHeader('X-Socket-Id', this.socketId());
					}
				}
			});
		}
	}
}
